package com.fan.play.ui

import android.app.Activity
import android.os.Bundle
import androidx.multidex.MultiDexApplication
import com.fan.play.manager.ActivityStack
import com.fan.play.media.MediaEngineAgent
import com.fan.play.utils.AppUtil

class XFPlayerApp : MultiDexApplication() {

    private var activityLifecycleCallback = object : ActivityLifecycleCallbacks {
        override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
            ActivityStack.add(activity)
        }

        override fun onActivityStarted(activity: Activity) {

        }

        override fun onActivityResumed(activity: Activity) {
        }

        override fun onActivityPaused(activity: Activity) {
        }

        override fun onActivityStopped(activity: Activity) {
        }

        override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
        }

        override fun onActivityDestroyed(activity: Activity) {
            ActivityStack.remove(activity)
        }
    }

    override fun onCreate() {
        super.onCreate()

        AppUtil.init(this)
        MediaEngineAgent.create(this)

        registerActivityLifecycleCallbacks(activityLifecycleCallback)
    }

    override fun onTerminate() {
        unregisterActivityLifecycleCallbacks(activityLifecycleCallback)

        MediaEngineAgent.destroy()

        super.onTerminate()
    }

}